﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Transactions;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using PetShop.Web.Entities;
using PetShop.Web.WebComponents;

namespace PetShop.Web
{
    public partial class WebFormCreateAccount : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void SubmitClicked(object sender, ImageClickEventArgs e)
        {
            if (Page.IsValid)
            {
                string userId = CleanString.InputText(txtUserId.Text, 50);
                string password = CleanString.InputText(txtPassword.Text, 50);
                string email = CleanString.InputText(txtEmail.Text, 50);
                AddressInfo address = addr.Address;
                string language = prefs.Language;
                string favCategory = prefs.Category;
                bool showFavorites = prefs.IsShowFavorites;
                bool showBanners = prefs.IsShowBanners;


                var newAccountInfo = new AccountInfo(userId, password, email, address, language, favCategory,
                                                     showFavorites, showBanners);

                try
                {
                    using (var ts = new TransactionScope(TransactionScopeOption.Required))
                    {
                        using (var cmd = new SqlCommand())
                        {
                            cmd.CommandText = "INSERT INTO Account " +
                                              " VALUES(@UserId, @Email, @FirstName, @LastName,  @Address1, @Address2," +
                                              " @City, @State, @Zip, @Country, @Phone,@Language, @Category, @ShowFavorites, " +
                                              " @ShowBanners, @Password)";
                            cmd.CommandType = CommandType.Text;

                            cmd.Parameters.AddWithValue("@Email", newAccountInfo.Email);
                            cmd.Parameters.AddWithValue("@FirstName", newAccountInfo.Address.FirstName);
                            cmd.Parameters.AddWithValue("@LastName", newAccountInfo.Address.LastName);
                            cmd.Parameters.AddWithValue("@Address1", newAccountInfo.Address.Address1);
                            cmd.Parameters.AddWithValue("@Address2", newAccountInfo.Address.Address2);
                            cmd.Parameters.AddWithValue("@City", newAccountInfo.Address.City);
                            cmd.Parameters.AddWithValue("@State", newAccountInfo.Address.State);
                            cmd.Parameters.AddWithValue("@Zip", newAccountInfo.Address.Zip);
                            cmd.Parameters.AddWithValue("@Country", newAccountInfo.Address.Country);
                            cmd.Parameters.AddWithValue("@Phone", newAccountInfo.Address.Phone);
                            cmd.Parameters.AddWithValue("@UserId", newAccountInfo.UserId);
                            cmd.Parameters.AddWithValue("@Language", newAccountInfo.Language);
                            cmd.Parameters.AddWithValue("@Category", newAccountInfo.Category);
                            cmd.Parameters.AddWithValue("@ShowFavorites", newAccountInfo.IsShowFavorites);
                            cmd.Parameters.AddWithValue("@ShowBanners", newAccountInfo.IsShowBanners);
                            cmd.Parameters.AddWithValue("@Password", newAccountInfo.Password);
                            using (
                                var conn =
                                    new SqlConnection(ConfigurationManager.AppSettings["SQLConnString"]))
                            {
                                cmd.Connection = conn;
                                conn.Open();
                                cmd.ExecuteNonQuery();
                            }
                        }

                        ts.Complete();
                    }

                    HttpContext.Current.Session["ACCOUNT_KEY"] = newAccountInfo;
                    FormsAuthentication.SetAuthCookie(newAccountInfo.UserId, false);
                    HttpContext.Current.Response.Redirect("MyAccount.aspx?action=create", true);
                }
                catch (Exception ex)
                {
                    valUserId.ErrorMessage = ex.Message;
                    valUserId.IsValid = false;
                }
            }
        }
    }
}